/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.satish.gmailapp.managedBeans;

import com.google.api.services.gmail.model.Message;
import com.google.api.services.gmail.model.MessagePartHeader;
import com.satish.gmailapp.beans.SearchCriteria;
import com.satish.gmailapp.beans.User;
import com.satish.gmailapp.service.GmailService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.List;
import java.util.ResourceBundle;

import static com.satish.gmailapp.constants.WebConstants.USER;

/**
 *
 * @author satish
 */
@ManagedBean(name = "gmail")
@RequestScoped
public class GmailManagedBean implements Serializable {
    private static final Logger logger = LogManager.getLogger(GmailManagedBean.class);
    
    private SearchCriteria criteria = new SearchCriteria();
    
    private final FacesContext facesContext = FacesContext.getCurrentInstance();
    private final ResourceBundle msgBundle = facesContext.getApplication().getResourceBundle(facesContext, "msg");

    @ManagedProperty(value = "#{gmailService}")
    private GmailService gmailService;
    
    public List<Message> getMessageList() throws IOException {
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
        User user = (User)session.getAttribute(USER);
        return gmailService.getMessages(user.getCredential());
    }
        
    public void trashMessages() throws IOException {
        HttpSession session = (HttpSession) facesContext.getExternalContext().getSession(false);
        User user = (User)session.getAttribute(USER);
        int count = gmailService.trashMessages(criteria, user.getCredential());
        
        String msg = MessageFormat.format(msgBundle.getString("TrashSuccess"), count);
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, msg, null));

    }
    
    public String isViewActive(String currentPage, String passedPage) {
        if(currentPage.equals(passedPage)) {
            return "active";
        }
        return "";
    }
    
    public String getMessageHeader(Message message, String headerName) {
        List<MessagePartHeader> headers = message.getPayload().getHeaders();
        for(MessagePartHeader header : headers) {
            if(header.getName().equals(headerName)) {
                return header.getValue();
            }
        }
        return "";
    }
    
    public boolean isMessageUnread(Message message) {
        List<String> labelIds = message.getLabelIds();
        return labelIds.contains("UNREAD");
    }

    public GmailService getGmailService() {
        return gmailService;
    }

    public void setGmailService(GmailService gmailService) {
        this.gmailService = gmailService;
    }

    public SearchCriteria getCriteria() {
        return criteria;
    }

    public void setCriteria(SearchCriteria criteria) {
        this.criteria = criteria;
    }
}